package com.lys.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.commonutils.R;
import com.lys.eduservice.entity.EduTeacher;
import com.lys.eduservice.entity.vo.TeacherQueryVo;
import com.lys.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Lyyyys
 * @since 2021-02-07
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    //把service注入
    @Autowired
    private EduTeacherService eduteacherService;

    //1查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduteacherService.list(null);
        return R.ok().data("items",list);
    }

    //使用rest风格，来进行删除讲师，所以传一个ID值即可
    //要获取路径中的值需要@PathViriable这个注解
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacherById(@PathVariable String id){
        boolean flag = eduteacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value="分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(@PathVariable Long page,@PathVariable Long limit){
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        eduteacherService.page(pageParam,null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total",total).data("records",records);
    }

    /*
     * @MethodName:pageQuery
     * @Description:page当前页，limit每页记录数
     * @Params: [page, limit, teacherQuery]
     * @Return: com.lys.commonutils.R
     * @Author: Lyyyys
     * @Date: 2021/3/2 14:51
     */
    @ApiOperation(value="条件分页查询讲师列表")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false) TeacherQueryVo teacherQueryVo
            //RequestBody是要通过Json数据传teacherQuery数据
            ){
        //将当前页，和当前页的个数封装到page对象中
        List<EduTeacher> records = eduteacherService.pageEduTeacherQuery(page,limit,teacherQueryVo);
        long total = records.size();
        return R.ok().data("items",records).data("total",total);
    }
    @ApiOperation(value = "新增讲师")
    @PostMapping("addEduTeacher")
    public R addEduTeacher(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){
        return eduteacherService.save(teacher) ? R.ok() : R.error();
    }

    @ApiOperation(value = "根据Id查询讲师")
    @GetMapping("{id}")
    public R getEduTeacherById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){
        EduTeacher eduTeacher = eduteacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    /*
     * @MethodName:updateEduTeacherById
     * @Description:用@PostMapping和@PutMapping写法不同，用@PostMapping做的话
     * 可以直接传一个eduTeacher对象（此时@PostMapping注解查找到的eduTeacher不为空）
     * 然后内部会获取一个Id的值自动进行update。
     * 用@PutMapping不能直接通过@RequestBody进行修改，因为此时eduTeacher的Id值为空
     * 需要手动设置
     * @Params: [eduTeacher]
     * @Return: com.lys.commonutils.R
     * @Author: Lyyyys
     * @Date: 2021/3/3 21:16
     */
    @ApiOperation(value = "根据Id修改讲师")
    @PostMapping("updateEduTeacher")
    public R updateEduTeacherById(
            @ApiParam(name = "EduTeacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){
        return eduteacherService.updateById(eduTeacher) ? R.ok() : R.error();
    }
}

